<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 商户模型
 *
 * @author 	FangMinglong
 * @date 	2017-04-12 13:54
 * 
 * @version 1.1.0
 * @date	2017年5月3日 上午10:17:40
 * @desc 	继承基类模型
 */
class Mod_merchant extends MY_Model
{
	use models\common\MultipleLanguage;
	
	protected $table_mapper = [
			'master' => 'merchant',
	];
	
	protected $allowed_fields = [
			'master' => ['cover', 'image', 'images', 'license', 'name', 'abstract', 'summary', 
				'mobile', 'email', 'website', 'country', 'city', 'street', 'address', 'zipcode', 'latitude',
				'longitude', 'is_official', 'member_id', 'owner_id', 'status'],
	];
	
	use models\common\BinaryBitBasedStatus;
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		
		$this->multi_language_handler();
		
		$this->status_bit_config = [
				'master' => [
						// FIXED How to mark check result?
						// 启用禁用、是否审核（完毕）、是否审核通过
						// 有关审核的状态：
						// 是否审核（完毕）: check_over/check_doing, 完毕/审核中...
						// 是否审核通过: checked/unchecked, 通过/未通过, accepted/rejected
						// 对于【是否审核通过】，如果【是否审核（完毕）】取值是 “check_doing”，本项无效！
						// 【是否审核（完毕）】中的 “check_over” 就相当于是【是否审核通过】的开关，over是开，doing是关！
						'status_bit_length' => 3,
						'status_bit_positive_tags' => ['check_over', 'checked', 'enabled'],
						'status_bit_negative_tags' => ['check_doing', 'unchecked', 'disabled'],
				],
		];
	}
	
	/**
	 * 插入前处理器
	 *
	 * @desc  主要负责格式化一些字段
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_create_data_handler($data, $mapping = 'master')
	{
		if (isset($data['images'])) {
			$data['images'] = is_array($data['images'])
									? implode(',', $data['images'])
									: $data['images'];
		}
		if (isset($data['license'])) {
			$data['license'] = is_array($data['license'])
									? implode(',', $data['license'])
									: $data['license'];
		}
		// cover：封面从多图，但可指定
		if (empty($data['cover']) && isset($data['images'])) {
			$haystack = trim($data['images'], ',');
			$length = strpos($haystack, ',') ?: strlen($haystack);
			$data['cover'] = substr($haystack, 0, $length);
		}
		
		return $data;
	}
	
	/**
	 * 更新前处理器
	 *
	 * @desc  主要负责格式化一些字段 & 过滤掉多语言字段，确保只在默认语言下更改主表的多语言字段
	 * @param number $id
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_update_data_handler($id, $data, $mapping = 'master')
	{
		$data = $this->before_create_data_handler($data, $mapping);
		
		$language = $this->input->post('language');
		$default = $this->config->item('default_language');
		// 过滤掉多语言字段
		if ($language !== $default) {
			# $fields = $this->get_multi_language_info('touroute', 'master');
			$fields = $this->get_multi_language_info('fields', 'master');
			foreach ($fields as $field) {
				# Warning: Illegal offset type in isset or empty ??? $field mustn't be an array!
				# if (isset($data[$field])) { unset($data[$field]); }
				unset($data[$field]);
			}
		}
		
		return $data;
	}
	
	public function get_as_poi_by_country_city($country, $city)
	{
		$conditions = [
				['select', 'id, cover, name, mobile, email, website, latitude, longitude, is_official'],
				['where', ["`status` & 1 > 0", null, false]],
				
		];
		empty($country) OR $conditions[] = ['where', "country='$country'"];
		empty($city) OR $conditions[] = ['where', "city='$city'"];
		$array = $this->select_instances_by_conditions($conditions);
		
		return $array;
	}
	
	public function get_ids_by_country_city($country, $city)
	{
		$conditions = [
				['select', 'id'],
				['where', ["`status` & 1 > 0", null, false]],
		];
		empty($country) OR $conditions[] = ['where', "country='$country'"];
		empty($city) OR $conditions[] = ['where', "city='$city'"];
		$array = $this->select_instances_by_conditions($conditions);
		$array = array_column($array, 'id');
		
		return $array;
	}
	
	/**
	 * 根据用户ID获取管辖范围内的商户ID列表
	 * 
	 * @desc    管辖范围暂遵循owner_id=member_id
	 * @author  Jason
	 * @date    2017年5月24日 上午11:03:49
	 * 
	 * @param   number $member_id
	 * 
	 * @return  array
	 */
	public function get_ids_by_member($member_id)
	{
		$conditions = [
				['select', 'id'],
				['where', "owner_id='$member_id'"],
		];
		$array = $this->select_instances_by_conditions($conditions);
		$array = array_column($array, 'id');
		
		return $array;
	}
	
}